<template>
  <div class="app-container">
    <el-row :gutter="20" class="height-auto">
      <el-col :span="8">
        <div class="search-container">
          <el-form ref="queryParamsRef" :inline="true" :model="queryParams" label-width="100px">
            <el-form-item label="流转时间">
              <el-date-picker v-model="dateRange" :unlink-panels="true" end-placeholder="结束日期"
                              range-separator="-" start-placeholder="开始日期" style="width: 240px;" type="daterange"
                              @change="timeChange"></el-date-picker>
            </el-form-item>
            <el-form-item label="姓名" prop="name">
              <el-input v-model="queryParams.name"/>
            </el-form-item>
            <el-form-item label="联系电话" prop="telephone">
              <el-input v-model="queryParams.telephone"/>
            </el-form-item>
            <el-form-item label="记录人" prop="createBy">
              <el-select v-model="queryParams.createBy" :placeholder="'请选择'" filterable style="width: 100%;"
                         @change="userChange">
                <el-option :key="userInfo.userId" :value="userInfo.userId" label="仅自己"/>
                <el-option :key="1" label="所有" value=""/>
              </el-select>
            </el-form-item>
            <el-form-item>
              <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery"/>
              <el-col :span="1.5">
                <el-button plain type="primary" @click="handleAdd">
                  <template
                    #icon>
                    <i-ep-plus/>
                  </template>
                  新增
                </el-button>
              </el-col>
            </el-form-item>
          </el-form>
        </div>
        <el-table v-adaptive :border="true" :data="onSiteAmbulanceStationList" :header-cell-style="$headerCellStyle"
                  :stripe="true" highlight-current-row @rowClick="handleUpdate">
          <el-table-column :align="'center'" fixed="left" label="姓名" prop="name" width="60"/>
          <el-table-column :align="'center'" :show-overflow-tooltip="true" label="年龄" prop="age" width="45"/>
          <el-table-column :align="'center'" :show-overflow-tooltip="true" label="国籍" prop="nationality" width="60">
            <template #default="scope">
              <dict-data-tag :options="dictDataMap['nationality_dict']" :value="scope.row.nationality"/>
            </template>
          </el-table-column>
          <el-table-column :align="'center'" :show-overflow-tooltip="true" label="性别" prop="sex" width="50">
            <template #default="scope">
              <dict-data-tag :options="dictDataMap['sys_user_sex']" :value="scope.row.sex"/>
            </template>
          </el-table-column>
          <el-table-column :align="'center'" label="航班号" prop="hangbanhao"/>
          <el-table-column :align="'center'" :show-overflow-tooltip="true" label="住址和单位" prop="address"
                           width="120"/>
          <el-table-column :align="'center'" :show-overflow-tooltip="true" label="联系电话" prop="telephone"
                           width="80"/>
          <el-table-column :align="'center'" label="通知部门" prop="tongzhibumen"/>
          <el-table-column :align="'center'" label="出诊部门" prop="chuzhenbumen">
            <template #default="scope">
              {{ scope.row.chuzhenbumen || scope.row.shijichuzhenweizhi }}
            </template>
          </el-table-column>
          <el-table-column :align="'center'" :show-overflow-tooltip="true" label="通知内容" prop="tongzhineirong"
                           width="100"/>
          <el-table-column :align="'center'" label="出诊位置" prop="chuzhenweizhi"/>
          <el-table-column :align="'center'" label="接警时间" prop="jiejingshijian" width="130">
            <template #default="scope">
              <span>{{ $parseTime(scope.row.jiejingshijian, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column :align="'center'" label="到达时间" prop="daodashijian" width="130">
            <template #default="scope">
              <span>{{ $parseTime(scope.row.daodashijian, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column :align="'center'" label="返回时间" prop="fanhuishijian" width="130">
            <template #default="scope">
              <span>{{ $parseTime(scope.row.fanhuishijian, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column :align="'center'" fixed="right" label="记录人" prop="createByName" width="55"/>

          <el-table-column :align="'center'" fixed="right" label="记录时间" prop="createTime" width="130">
            <template #default="scope">
              <span>{{ $parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
          <!--          <el-table-column label="操作" :align="'center'" class-name="small-padding fixed-width" width="220">-->
          <!--            <template #default="scope">-->
          <!--              <el-button-->
          <!--                type="success"-->
          <!--                @click="handleUpdate(scope.row)"-->
          <!--                v-hasPerm="['business:onSiteAmbulanceStation:edit']"-->
          <!--              >-->
          <!--                <template #icon>-->
          <!--                  <i-ep-edit/>-->
          <!--                </template>-->
          <!--                修改-->
          <!--              </el-button>-->
          <!--              <el-button-->
          <!--                type="danger"-->
          <!--                @click="handleDelete(scope.row)"-->
          <!--                v-hasPerm="['business:onSiteAmbulanceStation:remove']"-->
          <!--              >-->
          <!--                <template #icon>-->
          <!--                  <i-ep-delete/>-->
          <!--                </template>-->
          <!--                删除-->
          <!--              </el-button>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
        </el-table>

        <pagination
          v-show="total>0"
          v-model:limit="queryParams.pageSize"
          v-model:page="queryParams.pageNum"
          :total="total"
          @pagination="getList"
        />
      </el-col>
      <el-col :span="16" class="height-auto">
        <el-empty v-if="form.patientId === undefined" description="请点击左侧需要登记或者打印的数据行"></el-empty>
        <template v-else>
          <el-form ref="formRef" :model="form" :rules="rules" class="height-auto">
            <div class="a4-paper-responsive">
              <div style="position: absolute; top: 10px; z-index: 2">
                <el-button
                  v-if="form.createByName === userInfo.nickName || !form.onSiteAmbulanceStationId ||userInfo.userId === '1872110187421962241'"
                  style="margin-left: 10px"
                  type="success" @click="submitForm">保 存
                </el-button>
                <el-button v-show="form.patientId !== undefined && form.patientId !== ''" style="margin-left: 10px"
                           type="primary" @click="printDiv">
                  <template #icon>
                    <i-ep-printer/>
                  </template>
                  打印
                </el-button>
                <el-button type="warning" @click="templateClick">
                  <svg-icon :icon-class="'informed-notice'" class="mr5"/>
                  记录模板
                </el-button>
                <el-button type="warning" @click="generateTemplate">
                  <svg-icon :icon-class="'save'" class="mr5"/>
                  生成模板
                </el-button>
              </div>
              <div style="justify-items: center">
                <h3>现场救护站门（急）诊记录</h3>
              </div>
              <el-row>
                <el-divider content-position="left"><span class="divider-css">▶基本信息◀</span></el-divider>
                <el-col :span="5">
                  <el-form-item label="姓名" label-width="80" prop="name">
                    <el-input v-model="form.name" class="width-120" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="年龄" label-width="80" prop="age">
                    <el-input v-model="form.age" class="width-120"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="国籍" label-width="80" prop="nationality">
                    <el-select v-model="form.nationality" filterable placeholder="请选择国籍" style="width: 100%;">
                      <el-option v-for="dict in dictDataMap['nationality_dict']" :key="dict.dictValue"
                                 :label="dict.dictLabel" :value="dict.dictValue"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="性别" label-width="80" prop="sex">
                    <el-select v-model="form.sex" placeholder="请选择性别" style="width: 100%;">
                      <el-option v-for="dict in dictDataMap['sys_user_sex']" :key="dict.dictValue"
                                 :label="dict.dictLabel" :value="dict.dictValue"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="身份" label-width="80" prop="shenfen">
                    <el-input v-model="form.shenfen" class="width-120"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="航班号" label-width="80" prop="hangbanhao">
                    <el-input v-model="form.hangbanhao" class="width-120"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="陪同情况" label-width="80" prop="peitongqingkuang">
                    <el-input v-model="form.peitongqingkuang" class="width-120"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="门诊号" label-width="80" prop="clinicNo">
                    <el-input v-model="form.clinicNo" disabled style="width: 140px"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="10">
                  <el-form-item label="联系地址" label-width="80" prop="address">
                    <el-input v-model="form.address"/>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="联系人" label-width="80" prop="lianxiren">
                    <el-input v-model="form.lianxiren" class="width-120"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="联系电话" label-width="80" prop="telephone">
                    <el-input v-model="form.telephone" class="width-120"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="接警时间" label-width="80" prop="jiejingshijian">
                    <el-date-picker
                      v-model="form.jiejingshijian"
                      clearable
                      placeholder="请选择开始时间"
                      date-format="MMM DD, YYYY"
                      format="YYYY-MM-DD HH:mm"
                      time-format="HH:mm"
                      value-format="YYYY-MM-DD HH:mm:ss"
                      type="datetime"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="通知部门" label-width="80" prop="tongzhibumen">
                    <el-input v-model="form.tongzhibumen" class="width-120"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="14">
                  <el-form-item label="通知内容" label-width="80" prop="tongzhineirong">
                    <el-input v-model="form.tongzhineirong"/>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="出诊站室" label-width="80" prop="chuzhenweizhi">
                    <el-input v-model="form.chuzhenweizhi"/>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="出动时间" label-width="80" prop="chudongshijian">
                    <el-date-picker
                      v-model="form.chudongshijian"
                      date-format="MMM DD, YYYY"
                      format="YYYY-MM-DD HH:mm"
                      time-format="HH:mm"
                      type="datetime"
                      value-format="YYYY-MM-DD HH:mm:ss"
                      clearable
                      placeholder="请选择出动时间"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="到达现场时间" label-width="100" prop="daodashijian">
                    <el-date-picker
                      v-model="form.daodashijian"
                      clearable
                      placeholder="请选择开始时间"
                      date-format="MMM DD, YYYY"
                      format="YYYY-MM-DD HH:mm"
                      time-format="HH:mm"
                      value-format="YYYY-MM-DD HH:mm:ss"
                      type="datetime"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="返回时间" label-width="80" prop="fanhuishijian">
                    <el-date-picker
                      v-model="form.fanhuishijian"
                      clearable
                      placeholder="请选择开始时间"
                      date-format="MMM DD, YYYY"
                      format="YYYY-MM-DD HH:mm"
                      time-format="HH:mm"
                      value-format="YYYY-MM-DD HH:mm:ss"
                      type="datetime"
                    />
                  </el-form-item>
                </el-col>

                <el-col :span="5">
                  <el-form-item label="实际出诊位置" label-width="100" prop="shijichuzhenweizhi">
                    <el-input v-model="form.shijichuzhenweizhi"/>
                  </el-form-item>
                </el-col>
                <el-col :span="9">
                  <el-form-item label="病史提供者" label-width="80" prop="bingshitigongzhe">
                    <el-input v-model="form.bingshitigongzhe"/>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="可靠程度" label-width="80" prop="kekaochengdu">
                    <el-select v-model="form.kekaochengdu" placeholder="请选择可靠程度" style="width: 100%;">
                      <el-option key="1" label="可靠" value="可靠"></el-option>
                      <el-option key="1" label="较可靠" value="较可靠"></el-option>
                      <el-option key="1" label="不可靠" value="不可靠"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="主诉" label-width="80" prop="zhusu">
                    <el-input v-model="form.zhusu" rows="3" type="textarea"/>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="现病史" label-width="80" prop="xianbingshi">
                    <el-input v-model="form.xianbingshi" rows="5" type="textarea"/>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="既往史" label-width="80" prop="jiwangshi">
                    <el-input v-model="form.jiwangshi" rows="3" type="textarea"/>
                  </el-form-item>
                </el-col>
                <el-divider content-position="left"><span class="divider-css">▶体格检查◀</span></el-divider>
                <el-col :span="24">
                  <el-form-item>
                    <el-col :span="4">
                      <el-form-item class="tigejiancha" label="T:" label-width="80px" prop="tiwen">
                        <el-input v-model="form.tiwen" class="tigejiancha-input" clearable placeholder="体温"
                                  style="width:120px">
                          <template #append>℃</template>
                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="4">
                      <el-form-item class="tigejiancha" label="P:" label-width="80px" prop="maibo">
                        <el-input v-model="form.maibo" class="tigejiancha-input" clearable placeholder="脉搏"
                                  style="width:120px">
                          <template #append>次/分</template>
                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="5">
                      <el-form-item class="tigejiancha" label="R:" label-width="120px" prop="huxi">
                        <el-input v-model="form.huxi" class="tigejiancha-input" clearable placeholder="呼吸"
                                  style="width:120px">
                          <template #append>次/分</template>
                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="7">
                      <el-form-item class="tigejiancha" label="BP:" label-width="115px">
                        <el-input v-model="form.systolicPressure" class="tigejiancha-input" clearable
                                  placeholder="收缩压" style="width:50px"/>
                        <span style="margin:0 10px 0 25px">/</span>
                        <el-input v-model="form.diastolicPressure" class="tigejiancha-input" clearable
                                  placeholder="舒张压" style="width:50px">
                          <template #append>mmHg</template>
                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="3">
                      <el-form-item class="tigejiancha" label="Sp02:" label-width="135px" prop="sp02">
                        <el-input v-model="form.sp02" class="tigejiancha-input" clearable placeholder=""
                                  style="width:120px">
                        </el-input>
                      </el-form-item>
                    </el-col>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="意识状态" label-width="80" prop="shenzhi">
                    <el-input v-model="form.shenzhi"/>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item class="tigejiancha" label="瞳孔直径大小" label-width="90" prop="sex">
                    <el-form-item class="tigejiancha">
                      <el-input v-model="form.tongkongzhijingzuo" class="tigejiancha-input" clearable
                                placeholder="左" style="width:50px"/>
                      <span style="margin:0 10px 0 25px">/</span>
                      <el-input v-model="form.tongkongzhijingyou" class="tigejiancha-input" clearable
                                placeholder="右" style="width:50px">
                      </el-input>
                    </el-form-item>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="对光反射" label-width="80" prop="duiguangfanshe">
                    <el-input v-model="form.duiguangfanshe"/>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="其他体征" label-width="80" prop="qitatizhen">
                    <el-input v-model="form.qitatizhen"/>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="辅助检查" label-width="80" prop="fuzhujiancha">
                    <el-input v-model="form.fuzhujiancha"/>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="初步诊断" label-width="80" prop="chubuzhenduan">
                    <el-input v-model="form.chubuzhenduan"/>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="救治记录" label-width="80" prop="jiuzhenjilu">
                    <el-input v-model="form.jiuzhenjilu" rows="3" show-word-limit type="textarea"/>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="是否交接">
                    <el-radio-group v-model="form.isHandover" @change="changeIsHandover">
                      <el-radio v-for="dict in dictDataMap['sys_yes_no']" :key="dict.dictValue"
                                :value="dict.dictValue">{{
                          dict.dictLabel
                        }}
                      </el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-col>
                <el-col v-if="form.isHandover==='Y'" :span="12">
                  <el-form-item label="交接医院" label-width="80" prop="jiaojieyiyuan">
                    <el-input v-model="form.jiaojieyiyuan"/>
                  </el-form-item>
                </el-col>
                <el-col v-if="form.isHandover==='Y'" :span="12">
                  <el-form-item label="交接时间" label-width="80" prop="jiaojieshijian">
                    <el-date-picker
                      v-model="form.jiaojieshijian"
                      clearable
                      placeholder="请选择交接时间"
                      date-format="MMM DD, YYYY"
                      format="YYYY-MM-DD HH:mm"
                      time-format="HH:mm"
                      value-format="YYYY-MM-DD HH:mm:ss"
                      type="datetime"
                    />
                  </el-form-item>
                </el-col>
                <el-col v-if="form.isHandover==='Y'" :span="24">
                  <el-form-item label="交接时生命体征">
                    <el-col :span="4">
                      <el-form-item class="tigejiancha" label="T:" label-width="80px" prop="tiwen">
                        <el-input v-model="form.tiwenlast" class="tigejiancha-input" clearable placeholder="体温"
                                  style="width:120px">
                          <template #append>℃</template>
                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="4">
                      <el-form-item class="tigejiancha" label="P:" label-width="80px" prop="maibo">
                        <el-input v-model="form.maibolast" class="tigejiancha-input" clearable placeholder="脉搏"
                                  style="width:120px">
                          <template #append>次/分</template>
                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="5">
                      <el-form-item class="tigejiancha" label="R:" label-width="120px" prop="huxi">
                        <el-input v-model="form.huxilast" class="tigejiancha-input" clearable placeholder="呼吸"
                                  style="width:120px">
                          <template #append>次/分</template>
                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="7">
                      <el-form-item class="tigejiancha" label="BP:" label-width="115px">
                        <el-input v-model="form.systolicPressurelast" class="tigejiancha-input" clearable
                                  placeholder="收缩压" style="width:50px"/>
                        <span style="margin:0 10px 0 25px">/</span>
                        <el-input v-model="form.diastolicPressurelast" class="tigejiancha-input" clearable
                                  placeholder="舒张压" style="width:50px">
                          <template #append>mmHg</template>
                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="3">
                      <el-form-item class="tigejiancha" label="Sp02:" label-width="135px" prop="sp02">
                        <el-input v-model="form.sp02last" class="tigejiancha-input" clearable placeholder=""
                                  style="width:120px">
                        </el-input>
                      </el-form-item>
                    </el-col>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="出诊医生" label-width="80" prop="chuzhenyisheng">
                    <el-input v-model="form.chuzhenyisheng"/>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="出诊护士" label-width="80" prop="chuzhenhushi">
                    <el-input v-model="form.chuzhenhushi"/>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="出诊驾驶员" label-width="80" prop="chuzhenjiashiyuan">
                    <el-input v-model="form.chuzhenjiashiyuan"/>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="签名" label-width="80" prop="qianming">
                    <el-input v-model="form.qianming"/>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="收费情况" label-width="80" prop="shoufeiqingkuang">
                    <el-input v-model="form.shoufeiqingkuang"/>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="其他情况" label-width="80" prop="qitaqingkuang">
                    <el-input v-model="form.qitaqingkuang"/>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="病情程度" label-width="80" prop="bingqingpanduan">
                    <el-input v-model="form.bingqingpanduan"/>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="流病调查" label-width="80" prop="liubingdiaocha">
                    <el-radio-group v-model="form.liubingdiaocha">
                      <el-radio v-for="dict in dictDataMap['sys_yes_no']" :key="dict.dictValue"
                                :value="dict.dictValue">{{ dict.dictLabel }}
                      </el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-form>
        </template>
      </el-col>
    </el-row>
    <el-dialog v-model="templateOpen" :close-on-click-modal="false" :title="templateTitle" append-to-body width="90%"
               @close="templateCancel">
      <template-quote ref="templateQuoteRef" :query-params="templateParams"
                      @quote-ok-index="quoteOkIndex"/>
    </el-dialog>
    <el-dialog v-model="open" :close-on-click-modal="false" append-to-body draggable title="生成模板"
               width="500px">
      <el-form ref="formTemplateRef" :model="templateForm" :rules="rulesTemplate" label-width="100px">
        <el-form-item label="权限" prop="authorityType">
          <el-select v-model="templateForm.authorityType">
            <el-option v-for="dict in dictDataMap[
                                'template_authority_type'
                            ]" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模板名称" prop="title">
          <el-input v-model="templateForm.title" placeholder="请输入模板名称" @input="titleInput"/>
        </el-form-item>
        <el-form-item label="适用科室" prop="deptId">
          <base-dept-tree :org-id="templateForm.orgId" :value="templateForm.deptId" style="width: 100%;"
                          @deptSelect="deptSelect"/>
        </el-form-item>
        <el-form-item label="启用状态">
          <el-radio-group v-model="templateForm.startFlag">
            <el-radio v-for="dict in dictDataMap['sys_yes_no']" :key="dict.dictValue"
                      :value="dict.dictValue">{{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
                    <span class="dialog-footer">
                        <el-button type="primary" @click="submitFormTemplate">确 定</el-button>
                        <el-button @click="cancelTemplate">取 消</el-button>
                    </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {
  addOnSiteAmbulanceStation,
  delOnSiteAmbulanceStation,
  getOnSiteAmbulanceStation,
  selectPageOnSiteAmbulanceStation,
  updateOnSiteAmbulanceStation
} from "@/api/recordRegistration/onSiteAmbulanceStation.ts";
import {useUserStoreHook} from "@/store/modules/user.ts";
import templateQuote from "./templateQuote.vue";
import {useDictStoreHook} from "@/store/modules/dict"
import {firstLetterResult} from "@/utils/pinyin";
import {saveAmbulanceStationMasterAndDetail} from "@/api/patient/templateMaster.ts";

const dictStore = useDictStoreHook()
let dictDataMap = ref(dictStore.isTypeGetData('sys_yes_no', 'template_authority_type', 'sys_user_sex', 'nationality_dict'))

const {proxy} = getCurrentInstance() as any;
const useUserStore = useUserStoreHook();

// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo
const onSiteAmbulanceStationList = ref<any>([])
const date = proxy.$parseTime(new Date(), "{y}-{m}-{d} ");
const dateRange = ref([date + "00:00:00", date + "23:59:59"]);
const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
  name: "",
  orgId: userInfo.orgId,
  telephone: "",
  createBy: userInfo.userId
})
const total = ref(0)
const formRef = ref()
const form = ref<any>({
  name: "",
  age: "",
  sex: "",
  nationality: "CN",
  shenfen: "",
  hangbanhao: "",
  peitongqingkuang: "",
  address: "",
  lianxiren: "",
  qitatizhen: "",
  jiaojieyiyuan: "",
  jiaojieshijian: "",
  jiuzhenjilu: "",
  chudongshijian: "",
  telephone: "",
  tongzhibumen: "",
  fuzhujiancha: "",
  shoufeiqingkuang: "",
  qitaqingkuang: "",
  shijichuzhenweizhi: "",
  chuzhenbumen: "",
  chuzhenweizhi: "",
  jiejingshijian: "",
  daodashijian: "",
  fanhuishijian: "",
  bingshitigongzhe: "",
  kekaochengdu: "",
  zhusu: "",
  xianbingshi: "",
  jiwangshi: "",
  tiwen: "",
  maibo: "",
  huxi: "",
  systolicPressure: "",
  diastolicPressure: "",
  sp02: "",
  tiwenlast: "",
  maibolast: "",
  huxilast: "",
  systolicPressurelast: "",
  diastolicPressurelast: "",
  sp02last: "",
  shenzhi: "",
  duiguangfanshe: "",
  tongkongzhijingzuo: "",
  tongkongzhijingyou: "",
  chubuzhenduan: "",
  liubingdiaocha: "",
  bingqingpanduan: "",
  chuzhenyisheng: "",
  chuzhenhushi: "",
  chuzhenjiashiyuan: "",
  qianming: "",
  isHandover: "N"
})

const rules = ref<any>({
  name: [
    {
      required: true, message: "姓名不能为空", trigger: "blur"
    }
  ],
})

const templateTitle = ref<string>("")
// 、模板弹出框 关闭打开状态
const templateOpen = ref<boolean>(false)
const templateQuoteRef = ref()
const templateParams = ref<any>({
  toStation: true,
  pageNum: 1,
  pageSize: 10,
  type: "10",
  authorityType: userInfo.customerType != '3' ? '' : "0",
  title: "",
  titleInput: "",
  deptId: userInfo.loginDeptId,
  orgId: userInfo.orgId,
})

/** 用户点击事件 */
function userChange(value) {
  if (!value) {
    queryParams.value.createBy = ''
  } else {
    queryParams.value.createBy = value
  }
  getList()
}

// 生成模板弹出对话框
const formTemplateRef = ref();
const open = ref<boolean>(false)
const templateForm = ref<any>({
  authorityType: "2",
  title: "",
  titleInput: "",
  deptId: proxy.$currentSelectDept,
  deptName: proxy.$currentSelectDeptName,
  orgId: userInfo.orgId,
  startFlag: "Y",
  type: "10",
})
const rulesTemplate = {
  authorityType: [
    {
      required: true,
      message: "权限不能为空",
      trigger: "change",
    },
  ],
  title: [
    {
      required: true,
      message: "模板主题名称不能为空",
      trigger: "blur",
    },
  ],
  startFlag: [
    {
      required: true,
      message: "状态不能为空",
      trigger: "blur",
    },
  ],
  deptId: [
    {
      required: true,
      message: "科室不能为空",
      trigger: "blur",
    },
  ],
}

function changeIsHandover(value) {
  if (value === 'N') {
    form.value.jiaojieyiyuan = ""
    form.value.jiaojieshijian = ""
    form.value.tiwenlast = ""
    form.value.maibolast = ""
    form.value.huxilast = ""
    form.value.systolicPressurelast = ""
    form.value.diastolicPressurelast = ""
    form.value.sp02last = ""
  }
}

function timeChange(e) {
  if (e) {
    dateRange.value = [proxy.$parseTime(e[0], "{y}-{m}-{d} 00:00:00"), proxy.$parseTime(e[1], "{y}-{m}-{d} 23:59:59")]
  }
  getList()
}


function handleQuery() {
  getList()
}

function resetQuery() {
  queryParams.value.name = ""
  queryParams.value.telephone = ""
  reset()
  getList()
}

function handleAdd() {
  reset()
  form.value.patientId = ''

}

/** 查询现场救护站门（急）诊记录(重庆江北)列表 */
function getList() {
  selectPageOnSiteAmbulanceStation(proxy.$addDateRange(queryParams.value, dateRange.value)).then((response: any) => {
    onSiteAmbulanceStationList.value = response.rows;
    total.value = response.total;
  });
}


/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _onSiteAmbulanceStationId = row.onSiteAmbulanceStationId
  getOnSiteAmbulanceStation(_onSiteAmbulanceStationId).then((response: any) => {
    form.value = response.data;
  });
}


/** 删除按钮操作 */
function handleDelete(row) {
  const _onSiteAmbulanceStationIds = row.onSiteAmbulanceStationId
  ElMessageBox.confirm('是否确认删除数据？',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    delOnSiteAmbulanceStation(_onSiteAmbulanceStationIds).then(() => {
      getList();
      ElMessage.success("删除成功");
    })
  })
}

/** 提交按钮 */
function submitForm() {
  (formRef.value as any).validate(valid => {
    if (valid) {
      ElMessageBox.confirm('是否确认保存？',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(() => {
        if (form.value.onSiteAmbulanceStationId != null) {
          updateOnSiteAmbulanceStation(form.value).then(() => {
            ElMessage.success("修改成功");
            reset();
            getList();
          })
        } else {
          addOnSiteAmbulanceStation(form.value).then(() => {
            ElMessage.success("新增成功");
            reset();
            getList();
          })
        }
      })
    }
  })
}

// 表单重置
function reset() {
  form.value = {
    onSiteAmbulanceStationId: undefined,
    name: "",
    age: "",
    sex: "",
    nationality: "CN",
    shenfen: "",
    hangbanhao: "",
    peitongqingkuang: "",
    address: "",
    lianxiren: "",
    qitatizhen: "",
    jiaojieyiyuan: "",
    jiaojieshijian: "",
    jiuzhenjilu: "",
    chudongshijian: "",
    telephone: "",
    tongzhibumen: "",
    fuzhujiancha: "",
    shoufeiqingkuang: "",
    qitaqingkuang: "",
    shijichuzhenweizhi: "",
    chuzhenbumen: "",
    chuzhenweizhi: "",
    jiejingshijian: "",
    daodashijian: "",
    fanhuishijian: "",
    bingshitigongzhe: "",
    kekaochengdu: "",
    zhusu: "",
    xianbingshi: "",
    jiwangshi: "",
    tiwen: "",
    maibo: "",
    huxi: "",
    systolicPressure: "",
    diastolicPressure: "",
    sp02: "",
    tiwenlast: "",
    maibolast: "",
    huxilast: "",
    systolicPressurelast: "",
    diastolicPressurelast: "",
    sp02last: "",
    shenzhi: "",
    duiguangfanshe: "",
    tongkongzhijingzuo: "",
    tongkongzhijingyou: "",
    chubuzhenduan: "",
    liubingdiaocha: "",
    bingqingpanduan: "",
    chuzhenyisheng: "",
    chuzhenhushi: "",
    chuzhenjiashiyuan: "",
    qianming: "",
    orgId: queryParams.value.orgId,
    patientId: undefined
  }
}

function printDiv() {
  let url = "/business/onSiteAmbulanceStation/print";
  const formData = new FormData()
  formData.append("onSiteAmbulanceStationId", form.value.onSiteAmbulanceStationId)
  formData.append("orgId", form.value.orgId)
  // pdf打印 、打印预览
  proxy.$printPdfPublic(url, formData).then(res => {
    if (res) {
    } else {
      console.error('打印失败')
    }
  })
}

/**
 * 病历模板
 */
function templateClick() {
  templateOpen.value = true
}

/**
 * 关闭历史、常用项目、模板弹出框
 */
function templateCancel() {
  // 清空模板明细信息
  nextTick(() => {
    templateQuoteRef.value.emptyItem
  })

  templateOpen.value = false
}

/***********************************  模板引用  **********************************/
function quoteOkIndex(obj: any) {
  debugger
  // 遍历 obj 的属性
  for (const key in obj) {
    if (Object.prototype.hasOwnProperty.call(form.value, key)) {
      form.value[key] = obj[key];
    }
  }
  templateCancel()
}

/**
 * 生成模板
 */
function generateTemplate() {
  (formRef.value as any).validate(valid => {
    if (valid) {
      resetTemplate()
      open.value = true
    }
  })
}

function resetTemplate() {
  templateForm.value.title = ""
  templateForm.value.titleInput = ""
  templateForm.value.deptId = ""
  templateForm.value.deptName = ""
  templateForm.value.authorityType = "2"
  templateForm.value.startFlag = "Y"
  templateForm.value.type = "10"
  templateForm.value.orgId = userInfo.orgId
}

function cancelTemplate() {
  open.value = false
  resetTemplate()
}

// 主题生成拼音码
function titleInput(value) {
  if (value) {
    templateForm.value.titleInput = firstLetterResult(value)
  } else {
    templateForm.value.titleInput = ""
  }
}

// 科室点击事件
function deptSelect(node) {
  templateForm.value.deptId = node.deptId
  templateForm.value.deptName = node.deptName
}

/**
 * 生成模板保存
 */
function submitFormTemplate() {
  (formTemplateRef.value as any).validate(valid => {
    if (valid) {
      let saveForm = form.value
      saveForm.title = templateForm.value.title
      saveForm.titleInput = templateForm.value.titleInput
      saveForm.deptId = templateForm.value.deptId
      saveForm.deptName = templateForm.value.deptName
      saveForm.authorityType = templateForm.value.authorityType
      saveForm.startFlag = templateForm.value.startFlag
      saveForm.type = "10"
      saveForm.orgId = userInfo.orgId
      saveAmbulanceStationMasterAndDetail(saveForm).then(() => {
        ElMessage.success("模板生成成功")
        cancelTemplate()
      })
    }
  })
}


getList()
</script>

<script lang="ts">
export default {
  name: "OnSiteAmbulanceStation",
}
</script>

<style scoped>
.a4-paper-responsive {
  height: calc(100% - 20px); /* 高度自适应 */
  border: 1px solid #027687;
  padding: 0 20px 0 20px;
  overflow-y: auto
}

:deep(.tigejiancha-input) {
  margin-left: -10px
}

:deep(.tigejiancha-input .el-input__wrapper .el-input__inner) {
  width: 60px
}

:deep(.el-input-group__append) {
  width: 40px;
  padding: 0 5px;
}

</style>

